package leedcode_path.treeNoDE;

public class 判断是否平衡二叉树 {
    public boolean isBalanced(TreeNode root) {
        if (root==null) {
            return true;
        }else {
            int i=Math.abs(lenght(root.left)-lenght(root.right));
            if (i>1) return false;
            return isBalanced(root.left)&&isBalanced(root.right);

        }





    }
    public  int lenght(TreeNode root){
        if (root==null) return 0;
        int left=lenght(root.left);
        int right=lenght(root.right);

        return Math.max(left,right)+1;
    }

    public static void main(String[] args) {
        判断是否平衡二叉树 x=new 判断是否平衡二叉树();

        TreeNode x11=new TreeNode(7);
        TreeNode x12=new TreeNode(8);
        TreeNode x21=new TreeNode(4,x11,null);
        TreeNode x22=new TreeNode(5);
        TreeNode x23=new TreeNode(6,null,x12);
        TreeNode x31=new TreeNode(2,x21,x22);
        TreeNode x32=new TreeNode(3,null,x23);
        TreeNode x41=new TreeNode(1,x31,x32);
        boolean b=x.isBalanced(x41);
        System.out.println(b);

    }
}
